<?php

namespace app\appv110\controller;

use app\appv110\controller\CommonController;
use think\Validate;
use think\Db;
use app\util\Utils;
use app\appv110\model\Order;
use app\appv110\model\Store;
use app\appv110\model\Custom;
use app\appv110\model\CtmSpecial;
use app\util\Ocr;
use app\appv110\model\ItemToGoods;

class SmallstoreController extends CommonController {
    /*
     * code列表
     */

    public function code() {
        /*
         * 0  成功
         * 1  参数错误
         * 2  验证失败
         * 70000  
         * 70001  
         * 70002  
         * 70003  
         * 70004  
         * 70005  
         * 70006
         * 70007
         * 70008
         * 70009
         * 70010
         * 70011
         */
    }
    /*
     * i美优优首页（体验项目家居装）
     * author xfwed 
     * date 2020.08.28
     */

    public function index() {
        $rule = [
            'perPage' => 'integer|egt:1',
            'page' => 'integer|egt:0',
        ];
        $msg = [
            'perPage.integer' => '每页数量为数字',
            'perPage.egt' => '每页数量最小为1',
            'page.integer' => '页数为数字',
            'page.egt' => '页数最小为0',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }
        
        $perPage = isset($all['perPage']) ? $all['perPage'] : 20;
        $page = isset($all['page']) ? $all['page'] : 0;
        if ($page == 0) {
            $page = 1;
        }

        $offset = ($page - 1) * $perPage;
        $a = isset($a) ? $a : '';
        
        $where['i.is_del'] = 1;
//        $list = Item::where($where)
//                ->order('sell_count desc')
//                ->limit($offset, $perPage)
//                ->field('id,item_name,item_price,special_money,item_img,sell_count')
//                ->select();
        $list = ItemToGoods::alias('g')
                ->join('item i','i.id = g.item_id')
                ->where($where)
                ->order('i.sell_count desc')
                ->limit($offset, $perPage)
                ->group('g.item_id')
                ->field('i.id,i.item_name,i.item_price,i.special_money,i.item_img,i.sell_count')
                ->select();

        return Utils::ajaxReturn(0, 'success', $list);
        
    }
    
    /*
     * i美优优（体验项目家居装商品）
     * author xfwed 
     * date 2020.08.28
     */

    public function imGoods() {
        $rule = [
            'item_id' => 'require',
            'perPage' => 'integer|egt:1',
            'page' => 'integer|egt:0',
        ];
        $msg = [
            'item_id.require' => '项目id必须',
            'perPage.integer' => '每页数量为数字',
            'perPage.egt' => '每页数量最小为1',
            'page.integer' => '页数为数字',
            'page.egt' => '页数最小为0',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }
        
        $item_id = $all['item_id'];
        $shopId = input('param.shopId');//小店id  用于区分老板还是美容师
        $perPage = isset($all['perPage']) ? $all['perPage'] : 20;
        $page = isset($all['page']) ? $all['page'] : 0;
        if ($page == 0) {
            $page = 1;
        }

        $offset = ($page - 1) * $perPage;
        $a = isset($a) ? $a : '';
        
        $where['i.item_id'] = $item_id;
        $where['g.valid'] = 1;
        $where['g.verifyStatus'] = 2;
        $where['g.is_dis'] = 2;
        $where['g.is_smallshopsell'] = 1;
      
        if($shopId){
            $smallshop_type = Custom::where('id',$shopId)->value('smallshop_type');
            if($smallshop_type == 1){
                $pre = 0.4;
            }elseif ($smallshop_type == 2) {
                $pre = 0.6;
            }  else {
                $pre = 0.4;
            }
        }  else {
            $pre = 0.4;
        }
        
        $list = ItemToGoods::alias('i')
                ->join(['goods_info'=>'g'],'g.id = i.goods_info_id','left')
                ->join(['goods_info_detail'=>'d'],'d.goodsId = g.id','left')
                ->where($where)
                ->order('g.sell_count desc')
                ->group('i.goods_info_id')
                ->limit($offset, $perPage)
                ->field('g.id,g.name,g.icon,g.sell_count,d.oriPrice,d.price,d.leaveFactoryPrice,d.deduct_money')
                ->select();
        foreach ($list as $k => $v){
            $list[$k]['mybuy'] = bcmul($v['price'],0.12);
            $a = $v['price']*0.88;
            $a = bcsub($a,$v['leaveFactoryPrice']);
            $a = bcsub($a,$v['deduct_money']);
            $list[$k]['sharebuy'] = bcmul($a,0.4);
        }

        return Utils::ajaxReturn(0, 'success', $list);
        
    }
}
